local modulename = "abtestingDiversionArgPartyid"

local _M = {}
local mt = { __index = _M }
_M._VERSION = "0.0.1"

local log = require('abtesting.utils.log')
local ERRORINFO = require('abtesting.error.errcode').info

_M.new = function(self, database, policyLib)
    if not database then
        error { ERRORINFO.PARAMETER_NONE, 'need avaliable redis db' }
    end if not policyLib then
        error { ERRORINFO.PARAMETER_NONE, 'need avaliable policy lib' }
    end

    self.database = database
    self.policyLib = policyLib

    return setmetatable(self, mt)
end

_M.getUpstream = function(self, clientIP)
    local database = self.database
    local policyLib = self.policyLib

    log:debug("query redis: ", "policykey=" .. policyLib, ", clientIp=" .. clientIP)

    local upstream, err = database:hget(policyLib, clientIP)
    if not upstream then
        error { ERRORINFO.REDIS_ERROR, err }
    end

    if upstream == ngx.null then
        return nil
    else
        return upstream
    end

end

return _M
